package codej.language.java;

import static codej.language.java.Recognize.*;

import java.io.*;
import java.util.*;

import codej.parser.*;
import codej.parser.java.*;

public class RecognizeIndex {

	private static void buildNext(Scanner scannedIndex) {
		String sourcePath = userDir(scannedIndex.next());
		try {
			recognize(read(sourcePath));
		} catch (RuntimeException e) {
			System.err.println(sourcePath);
			System.err.println(e.getMessage());
			throw e;
		}
	}

	private static boolean continueBuilding(Scanner index) {
		return index.hasNext();
	}

	public static final void main(String... args) {
		new RecognizeIndex().run();
	}

	private static String read(String path) {
		return new ParserAgent(JavaParser.class).slurp(new File(path));
	}

	private static Scanner scanning(String path) {
		return new Scanner(read(path)).useDelimiter("\n");
	}

	private static String userDir(String path) {
		return System.getProperty("user.dir") + "/" + path;
	}

	public void run() {
		Scanner scannedIndex = scanning(userDir("src/test/resources/index.txt"));
		while (continueBuilding(scannedIndex)) {
			buildNext(scannedIndex);
		}
	}
}